package com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyAdapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.diudiutechnology.www.yadiudiu.R;
import com.diudiutechnology.www.yadiudiu.util.utils.Constant;
import com.diudiutechnology.www.yadiudiu.util.utils.RatingBar;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CallOrderBean;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CoolieActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.DetailsFruitsAndVegetablesActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.HousekeepingActivity;

import java.util.ArrayList;
import java.util.List;

import static com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CleaningActivity.CLEANING_BEAN;

/**
 * Created by Administrator on 2017/8/21.
 * 干洗列表
 */

public class CleaningActivityAdapter extends BaseAdapter {

    private Context mContext;

    List<CallOrderBean> model = new ArrayList<>();

    private String mPos;

    public CleaningActivityAdapter(Context context, List<CallOrderBean> model, String pos) {
        this.mContext = context;
        this.mPos = pos;
        this.model.addAll(model);
    }

    @Override
    public int getCount() {
        return model.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void addItem(List<CallOrderBean> model) {
        this.model = model;
        notifyDataSetChanged();
    }

    public void emptying() {
        this.model.clear();
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.item_cleaning, null);
            holder.item_image = (ImageView) convertView
                    .findViewById(R.id.item_image);
            holder.item_title = (TextView) convertView
                    .findViewById(R.id.item_title);
            holder.rating = (RatingBar) convertView
                    .findViewById(R.id.rating);
            holder.item_content = (TextView) convertView
                    .findViewById(R.id.item_content);
            holder.positioning_text = (TextView) convertView
                    .findViewById(R.id.positioning_text);
            holder.browse_text = (TextView) convertView
                    .findViewById(R.id.browse_text);
            holder.comments_text = (TextView) convertView
                    .findViewById(R.id.comments_text);
            holder.mGoDetail = (RelativeLayout) convertView.findViewById(R.id.go_detail);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final CallOrderBean bean = model.get(position);
        String[] split = bean.figurepic.split("\\|");
        if (split.length > 0) {
            Glide.with(mContext).load(Constant.HTTP + split[0]).into(holder.item_image);
        }
        holder.item_title.setText(bean.name);
        holder.item_content.setText(bean.profile);
        holder.positioning_text.setText(Double.toString(bean.km) + "km");
        holder.browse_text.setText(String.valueOf(bean.order));
        holder.comments_text.setText(String.valueOf(bean.countcomment));
        holder.rating.setClickable(false);//设置可否点击
        holder.rating.setStar((float) bean.star);//设置显示的星星个数
        holder.mGoDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                if (mPos.equals("0")) {
                    intent.setClass(mContext, CoolieActivity.class);//苦力
                    intent.putExtra("pos", "0");
                } else if (mPos.equals("1")) {//干洗
                    intent.setClass(mContext, DetailsFruitsAndVegetablesActivity.class);
                    intent.putExtra("pos", "0");
                } else if (mPos.equals("2")) {//开锁
                    intent.setClass(mContext, CoolieActivity.class);
                    intent.putExtra("pos", "1");
                } else if (mPos.equals("3")) {//家政
                    intent.setClass(mContext, HousekeepingActivity.class);
                } else {//果蔬
                    intent.setClass(mContext, DetailsFruitsAndVegetablesActivity.class);
                }
                intent.putExtra(CLEANING_BEAN, bean);
                mContext.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder {
        ImageView item_image;
        TextView item_title;
        RatingBar rating;
        TextView item_content;
        TextView positioning_text;
        TextView browse_text;
        TextView comments_text;
        RelativeLayout mGoDetail;
    }
}

